package com.abel.bigwater.kmgw.impl

import com.abel.bigwater.dataserver.model.ServiceConfig
import com.abel.bigwater.kmgw.dao.ConfigDao
import com.alibaba.fastjson.JSON
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.test.context.junit4.SpringRunner

@SpringBootTest(classes = [ConfigDaoImplTest::class])
@RunWith(SpringRunner::class)
@ComponentScan(basePackages = ["com.abel.bigwater"])
class ConfigDaoImplTest {

    @Autowired
    var configDao: ConfigDao? = null

    @Test
    fun getConfig() {
        val cfg = configDao!!.getConfig(KMING_SERVER_CFG_ID)
        lgr.info("config for ewide: ${JSON.toJSONString(cfg)}")
    }

    @Test
    fun createConfig() {
        val cfg = ServiceConfig().apply {
            cfgId = KMING_SERVER_CFG_ID
            localPort = KMING_PORT
            running = true
        }

        configDao?.deleteConfig(cfg.cfgId!!)
        Assert.assertEquals(1, configDao!!.createConfig(cfg))

        val cfg2 = configDao?.getConfig(KMING_SERVER_CFG_ID)
        Assert.assertEquals(cfg, cfg2)
    }

    companion object {
        const val KMING_SERVER_CFG_ID = "ewideServer"

        const val KMING_PORT = 6336

        private val lgr = LoggerFactory.getLogger(ConfigDaoImplTest::class.java)
    }
}